package org.xml.parser.DOM4J;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.Book;

public class DOM4JTest {
	private static int bookIndex = 0;
	private static Book b = null;
	private static ArrayList<Book> bookList = new ArrayList<Book>();

	public static void main(String[] args) throws IOException {
		dom4jParser();
	}

	public static void dom4jParser() throws IOException {
		// 1.创建一个SAXReader对象reader
		SAXReader reader = new SAXReader();
		try {
			// 2.通过reader对象的read方法加载xml文件，获取Document对象
			Document document = reader.read(new File("books1.xml"));
			Element bookStore = document.getRootElement();// 通过document对象获取根节点bookstore
//			// 获取根节点的子节点的相关信息
//			Iterator iterator = bookStore.elementIterator();// 通过Element对象的elementIterator方法获取迭代器
//			// 遍历迭代器,获取bookstore中的节点（book）
//			while (iterator.hasNext()) {
//				bookIndex++;
//				System.out
//						.println("******** 开始遍历第" + bookIndex + "本书 ********");
//				b = new Book();
//				Element book = (Element) iterator.next();
//				// 获取book的属性名和属性值
//				List<Attribute> bookAttrs = book.attributes();
//				for (Attribute attribute : bookAttrs) {
//					System.out.println("属性名：" + attribute.getName() + "-属性值："
//							+ attribute.getValue());
//					if ("id".equals(attribute.getName())) {
//						b.setId(attribute.getValue());
//					}
//				}
//				// 获取book的子节点
//				Iterator it = book.elementIterator();
//				while (it.hasNext()) {
//					Element bookChild = (Element) it.next();
//					System.out.println("节点名：" + bookChild.getName() + "-节点值："
//							+ bookChild.getStringValue());
//					if ("name".equals(bookChild.getName())) {
//						b.setName(bookChild.getStringValue());
//					} else if ("author".equals(bookChild.getName())) {
//						b.setAuthor(bookChild.getStringValue());
//					} else if ("year".equals(bookChild.getName())) {
//						b.setYear(bookChild.getStringValue());
//					} else if ("price".equals(bookChild.getName())) {
//						b.setPrice(bookChild.getStringValue());
//					} else if ("language".equals(bookChild.getName())) {
//						b.setLanguage(bookChild.getStringValue());
//					}
//				}
//				bookList.add(b); // 将book对象添加到bookList集合中
//				System.out.println("******** 结束遍历第 " + bookIndex
//						+ "本书 ********\n");
//			}
//			// 输出ArrayList中的book
//			System.out.println("ArrayList中一共有" + bookList.size() + "本书！");
//			for (Book book : bookList) {
//				System.out.println(book);
//			}
			Element book = bookStore.element("book");
			Element language = book.addElement("language");
			language.setText("简体中文");
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter writer = new XMLWriter(new FileOutputStream(
					"books_append_dom4j.xml"), format);
			writer.write(document);// 将文档写入到输出流
			writer.close();
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
